﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HIDB;
using Model;

namespace DB_Factory
{
    public class DB_Factory
    {
        private static dbo conn;
        private static List<dbo> connList;

        /// <summary>
        /// 获取配置的所有Connection
        /// </summary>
        public static List<dbo> ConnList
        {
            get { return DB_Factory.connList; }
            //set { DB_Factory.connList = value; }
        }
        private static dbo conn_default = common.make();

        /// <summary>
        /// 设置所有的Connection
        /// </summary>
        /// <param name="list"></param>
        public static void SetAllConnections(List<EtlTargetSystemInfo> list)
        {
            foreach (EtlTargetSystemInfo item in list)
            {
                if (connList == null)
                {
                    connList = new List<dbo>();
                }
                connList.Clear();
                connList.Add(common.make(DBType.SQLServer, "Server=" + conn_default.DbAddress + ";uid=" + item.DB_UserID + ";pwd=" + item.DB_PWD + ";database=" + conn_default.DbName + ";pooling=true"));
            }
            //return connList;
        }

        /// <summary>
        /// 获取当前合法连接
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static dbo GetConnection(string userName, string pwd)
        {
            //if (conn == null)
            //{
                conn = common.make(DBType.SQLServer, "Server=" + conn_default.DbAddress + ";uid=" + userName + ";pwd=" + pwd + ";database=" + conn_default.DbName + ";pooling=true");
            //}
            return conn;
        }
    }
}
